01-24-24 (Wednesday)

Dear Heavenly Father,
We invite Your presence into this classroom.
Let Your light shine upon us, illuminating the path of learning, so we may contemplate your beauty and love in everything you made.
May this classroom be a place of respect, fellowship, and growth. Help us to be open to new ideas, to embrace challenges, and to support one another in our academic endeavors. Bless this class so that it may be a space where minds are enriched, friendships are formed, and hearts are touched.
Guide our imaginations and desires towards your love and justice, so that we may respond adequately to your call to be Christ’s agents of renewal in the world.
Through our Lord Jesus Christ, your Son, who lives and reigns with you in the unity of the Holy Spirit, one God, for ever and ever.
Amen.

1 Announcements

  • Go through some feedback in Moodle
  • Explain Perusall grading

2 POGIL Activity - Arithmetic Expressions

  • Let’s finish Model 3

Click here to access the file.

3 Operators

Operation Operator Example Evaluates to
Addition + 2 + 2 4
Subtraction - 4 - 1 3
Multiplication * 1.5 * 2 3.0
Division / 5 / 2 2.5
Floor Division // 5 // 2 2
Modulus (remainder of division) % 5 % 2 1
Exponent ** 3**3 27

3.1 Resulting types

  • If any of the operands is a float, result will be a float. Otherwise (both are integer), result is an integer.

  • However, there is an exception: result of a division (not floor division) is always a float. Careful with that! (why? we’ll see in a moment)

3.2 Operator precedence

  • Always be careful with expressions using more than one operator! For example:
a = 3
b = 6
c = a + b * 2
print(c)
  • This evaluates as \(a + (b \times 2) = 3 + (6 * 2) = 3 + 12 = 15\)

Python operator precedence order:

  1. Parentheses: ()
  2. Exponents: **
  3. Multiplication, divisions and modulus: * / // %
  4. Addition and subtraction: + -
  5. Comparisons: <= < >= > == != is (next week)
  6. Boolean not (next week)
  7. Boolean and (next week)
  8. Boolean or (next week)

3.3 Operators in strings

  • Python also permits using SOME operators with strings. In a metaphorical way…
String Operation Metaphor Operator Example Evaluates to
Concatenation Addition + "Hey" + " " + "apple" "Hey apple"
Repetition Multiplication * "na" * 4 "nananana"
  • Other operators are not supported. Multiplication of a string with another string is also not supported. Both wouldn’t make so much sense…

3.4 Evaluating operations in strings

It is also possible to evaluate an expression coded as a string. For example:

expression = "2 * (4 + 6) / 3 - 5"
result = eval(expression)
print(result)
1.666666666666667

4 Overflow

  • Integers and floats are representations in the memory of your computer. Therefore, there are value limits to these numbers.
    • If we pass the limit, we arrive at an overflow, and the number is not computed correctly.
  • For integers, Python dynamically increases the use of memory as the number grows. Therefore, in theory there is a limit (computer memory will be full), but in practice this will almost never be the case.
    • For example, try 2147483647 ** 200
    • This, however, is a Python feature. Other languages may not handle that.
  • For floats, numbers are limited by the size of the mantissa and exponent.
    • Remember: for \(1.2345 \times 10^7\), \(1.2345\) is the mantissa, and \(7\) is the exponent
    • In Python, maximum is about 3 digits for the exponent and 17 digits for the mantissa
    • For example, try: 2.0**2000
      • OverflowError

More info at https://docs.python.org/3/tutorial/floatingpoint.html

4.1 So, be careful when mixing numeric types!

  • On June 4, 1996 the European Space Agency launched the first Ariane 5 rocket:
    • the result of a decade of development, $8 billion
    • Exploded 40 seconds after lift-off with a $500 million satellite payload on board…
  • Cause was a software error related to a number type conversion and an overflow

http://www-users.math.umn.edu/~arnold/disasters/ariane.html

5 Constants

  • Constants are variables with values that the program never changes
  • Reasons to use them:
    • improves readability of the source code
    • facilitates easier program modification
  • Good programming practice (style):
    • place all constant declarations at the beginning of a program
    • use all CAPITALS for constant names

Example: which code is more clear?

x = 3.14159
y = 1.324
z = 2*x*y
PI = 3.14159
radius = 1.324
circumference = 2*PI*radius

6 Math module

For other mathematical operations, you can import Python’s math module. Two ways to do that:

  • Import the whole module and use as needed: need to use the module name followed by a dot (.) before using a function or variable from the module.
import math
r = 3
circumference = 2 * r * math.pi
  • Import only the functions and variables you will need. Then, no need to use the qualifier “math.”
from math import pi
r = 3
circumference = 2 * r * pi

Some functions in the math module (documentation here)

Method Explanation
math.cos(x) Returns cosine of x (rads)
math.sin(x) Returns sine of x (rads)
math.tan(x) Returns tangent of x (rads)
math.degrees(x) Converts x radians to degrees
math.radians(x) Converts x degrees to rads
math.sqrt(x) Returns square root of x
  • You can also try to import all of the modules resources by using
from math import *
  • However, this is not recommended due to “namespace pollution” (having lots of names referenced that are actually not used)

7 Random module

  • Another useful module in Python is random, for generating random numbers. (Documentation)

For example:

# import the random module
import random
 
# Generates a random number between
# a given positive range
r1 = random.randint(5, 15)
print("Random number between 5 and 15 is % s" % (r1))
 
# Generates a random number between
# two given negative range
r2 = random.randint(-10, -2)
print("Random number between -10 and -2 is % d" % (r2))
Random number between 5 and 15 is 6
Random number between -10 and -2 is -5

8 Activity:

  • What’s a program to make our turtle draw random circles around the screen?